#include <gtest/gtest.h>

// 简单的示例测试
TEST(ExampleTest, BasicAssertion) {
    EXPECT_EQ(1 + 1, 2);
    EXPECT_TRUE(true);
    EXPECT_FALSE(false);
}

TEST(ExampleTest, StringComparison) {
    std::string str1 = "Hello";
    std::string str2 = "Hello";
    EXPECT_EQ(str1, str2);
    EXPECT_STREQ(str1.c_str(), str2.c_str());
}

TEST(ExampleTest, FloatingPointComparison) {
    double a = 0.1 + 0.2;
    double b = 0.3;
    EXPECT_NEAR(a, b, 1e-10);
}

// 测试异常
TEST(ExampleTest, ExceptionTest) {
    EXPECT_THROW({ throw std::runtime_error("test error"); }, std::runtime_error);

    EXPECT_NO_THROW({
        int x = 1 + 1;
        (void)x;
    });
}

// 参数化测试示例
class ParameterizedTest : public ::testing::TestWithParam<int> {};

TEST_P(ParameterizedTest, SquareTest) {
    int value = GetParam();
    EXPECT_GE(value * value, 0);
}

INSTANTIATE_TEST_SUITE_P(NumberTests, ParameterizedTest, ::testing::Values(1, 2, 3, 4, 5));

// Fixture测试示例
class FixtureTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 在每个测试前运行
        data = new int[10];
        for (int i = 0; i < 10; ++i) { data[i] = i; }
    }

    void TearDown() override {
        // 在每个测试后运行
        delete[] data;
    }

    int *data;
};

TEST_F(FixtureTest, ArrayTest) {
    EXPECT_EQ(data[0], 0);
    EXPECT_EQ(data[9], 9);
}

TEST_F(FixtureTest, ArrayModification) {
    data[5] = 100;
    EXPECT_EQ(data[5], 100);
}
